package cn.itcast.goods.user.service;

import java.io.IOException;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Properties;

import javax.mail.Session;
import javax.management.RuntimeErrorException;

import cn.itcast.commons.CommonUtils;
import cn.itcast.goods.user.dao.UserDao;
import cn.itcast.goods.user.domain.User;
import cn.itcast.mail.Mail;
import cn.itcast.mail.MailUtils;

public class UserService {

	private UserDao userDao=new UserDao();
/*
 * 检验指定登录名的用户是否存在
 * @param loginname
 * @return 
 * 
 * */
	public boolean validateLoignname(String loginname) {
		// TODO Auto-generated method stub
		try {
			return userDao.validateLoginname(loginname);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		
	}
	/*
	 * 检验指定的email会员是否存在
	 * @param email
	 * 
	 * @return
	 * */
	public boolean validateEmail(String email){
		try {
			return userDao.validateEmail(email);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	/*
	 * 注册
	 * 
	 * */
	public void regist(User user){
		
		try {
			/*
			 * 1.对user的数据进行补全
			 * */
			user.setUid(CommonUtils.uuid());
			user.setActivationCode(CommonUtils.uuid()+CommonUtils.uuid());
			user.setStatus(false);
			/*
			* 2.向数据库中添加数据 
			* */
			userDao.add(user);
			/*
			 * 向用户邮箱地址发送激活邮件
			 * 
			 * */
			//读取email模板中的数据
			Properties props=new Properties();
			props.load(this.getClass().getClassLoader().getResourceAsStream("email_template.properties"));
			String host=props.getProperty("host");//获取邮件服务器地址
			String username=props.getProperty("username");//获取用户名
			String password=props.getProperty("password");//获取密码
			String from=props.getProperty("from");//获取发件人地址
			String to=user.getEmail();//获取收件人地址
			String subject=props.getProperty("subject");//获取主题
			//获取内容模板，替换其中的激活码
			String content=MessageFormat.format(props.getProperty("content"), user.getActivationCode());
			
			//发送邮件
			Session session=MailUtils.createSession(host, username, password);
			Mail mail=new Mail(from ,to,subject,content);
			MailUtils.send(session, mail);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 邮件激活功能
	 * 
	 */
	public void activation(String activationCode) throws  UserException {
		// TODO Auto-generated method stub
		User user;
		try {
			//1.通过激活码查询用户
			user= userDao.findByActivationCode(activationCode);
			//2.如果没有查询到用户说明激活码无效
			if(user==null) throw new UserException("无效的几活码");
			//3.如果用户的状体已是激活状态，那么说明用户是第二次激活
			if(user.isStatus()) throw new UserException("您已经激活了，请不要二次激活");
			//4.修改用户状态为true，即为激活状态。
			userDao.updateStatus(user.getUid(),true);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		
	}
	/**
	 * 登录功能
	 * 
	 * */
	public User login(User formBean) {
		// TODO Auto-generated method stub
		
		try {
			return userDao.findByLoginnameAndLoginpass(formBean.getLoginname(), formBean.getLoginpass());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		
	}
	//修改密码
	public void updatePassword(String uid, String newpass) {
		// TODO Auto-generated method stub
		try {
			userDao.updatePassword(uid, newpass);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		
		
	}
	
}
